package com.ohos.carbon.slice;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.DataBean;
import com.ohos.carbon.slice.themes.CurrentThemeSlice;
import com.ohos.carbon.slice.themes.DarkAndLightSlice;
import com.ohos.carbon.slice.themes.WindowsThemesSlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;

import java.util.ArrayList;

import static com.ohos.carbon.slice.MainAbilitySlice.preferences;

public class ThemesSlice extends AbilitySlice {

    private Image back_button;
    private Text text_title, text;
    private ListContainer listContainer;
    private ArrayList<DataBean> mList;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_themes);
        initData();
        initView();
    }

    private void initData() {
        mList = new ArrayList<>();
        Element favour = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_star_border_black_24dp);
        Element starFavour = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_star_gray_24dp);
        mList.add(new DataBean(null, "What can be done by applying different themes", null, null));
        mList.add(new DataBean(null, "Dark and Light", null, preferences.getBoolean("Dark and Light", false) ? starFavour : favour));
        mList.add(new DataBean(null, "Current Theme", null, preferences.getBoolean("Current Theme", false) ? starFavour : favour));
        mList.add(new DataBean(null, "Windows Theme", null, preferences.getBoolean("Windows Theme", false) ? starFavour : favour));
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Themes");
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_listview);
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        listContainer.setItemProvider(new WhatsProvider(mList, getContext()));
        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                switch (i) {
                    case 1:
                        Intent intent = new Intent();
                        present(new DarkAndLightSlice(), intent);
                        break;
                    case 2:
                        Intent intent1 = new Intent();
                        present(new CurrentThemeSlice(), intent1);
                        break;
                    case 3:
                        Intent intent2 = new Intent();
                        present(new WindowsThemesSlice(), intent2);
                        break;
                }
            }
        });

        //返回上个页面
        back_button.setClickedListener(component -> {
            onBackPressed();
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
